function rs_init(N,K,m,p,b)
%
%Rs_init(N,K,m,p,b)
% 
% This function initializes the lookup tables that the other functions use,
% such as: 
%
% •	RS Generator Polynomial, based on N, K, b, and the primitive polynomial
% of the information codeword
% •	\alpha^i lookup table
% •	\alpha^-i lookup table
% •	log_(\alpha)(i) lookup table
% 
% After initializing those tables, rs_init saves them in a global variable, which is of struct type
% 
% Tansu Demirbilek
% ademirbi@mc.com
% Mercury Computer Systems


%% Create the generator polynomial based on the primitive polynomial
g = rsgenpoly(N,K,p,b);

%% Generate the alpha^i lookup table
% alpha_to contains: [alpha^1 alpha^2 ... alpha^n 0]
% Note: alpha^n = alpha^0 = 1
a = gf(2,m,p);
alpha_to = a.^[1:N];
%alpha_to(n+1) = gf(0,m,p);
alpha_to = alpha_to.x;

%% Generate alpha_inv lookup table
% alpha_inv contains [alpha^(-1) ... alpha^(-14) alpha^(-15)]
% or equivalently:      [alpha^14 alpha^13 ... alpha^1 alpha^0]
alpha_inv = alpha_to(N-1:-1:1);
alpha_inv(N) = alpha_to(N);

%% Generate y =log_alpha(x) lookup table
% log_alpha contains: [log_alpha(1) log_alpha(2) ... log_alpha(n)]
for i=1:N    log_alpha(i) = find(alpha_to == i); end

%% Create/Load the global GF variable.
gf_name = sprintf('GF_%d_%d_%d_%d_%d',N,K,m,b,p);
eval(sprintf('global GF_%d_%d_%d_%d_%d',N,K,m,b,p));

eval(sprintf('%s.p = p;',gf_name));
eval(sprintf('%s.g = g;',gf_name));
eval(sprintf('%s.alpha_to = alpha_to;',gf_name));
eval(sprintf('%s.alpha_inv = alpha_inv;',gf_name));
eval(sprintf('%s.log_alpha = log_alpha;',gf_name));


